home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Python / sysmodule.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  10KB  |  423 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* System module */
  33.  
  34. /*
  35. Various bits of information used by the interpreter are collected in
  36. module 'sys'.
  37. Function member:
  38. - exit(sts): raise SystemExit
  39. Data members:
  40. - stdin, stdout, stderr: standard file objects
  41. - modules: the table of modules (dictionary)
  42. - path: module search path (list of strings)
  43. - argv: script arguments (list of strings)
  44. - ps1, ps2: optional primary and secondary prompts (strings)
  45. */
  46.  
  47. #include "allobjects.h"
  48.  
  49. #include "sysmodule.h"
  50. #include "import.h"
  51. #include "modsupport.h"
  52. #include "osdefs.h"
  53.  
  54. #include "protos/sysmodule_protos.h"
  55.  
  56. object *sys_trace, *sys_profile;
  57. int sys_checkinterval = 10;
  58.  
  59. static object *sysdict;
  60.  
  61. #ifdef MS_COREDLL
  62. extern void *PyWin_DLLhModule;
  63. #endif
  64.  
  65. object *
  66. sysget(name)
  67.     char *name;
  68. {
  69.     return dictlookup(sysdict, name);
  70. }
  71.  
  72. FILE *
  73. sysgetfile(name, def)
  74.     char *name;
  75.     FILE *def;
  76. {
  77.     FILE *fp = NULL;
  78.     object *v = sysget(name);
  79.     if (v != NULL && is_fileobject(v))
  80.         fp = getfilefile(v);
  81.     if (fp == NULL)
  82.         fp = def;
  83.     return fp;
  84. }
  85.  
  86. int
  87. sysset(name, v)
  88.     char *name;
  89.     object *v;
  90. {
  91.     if (v == NULL) {
  92.         if (dictlookup(sysdict, name) == NULL)
  93.             return 0;
  94.         else
  95.             return dictremove(sysdict, name);
  96.     }
  97.     else
  98.         return dictinsert(sysdict, name, v);
  99. }
  100.  
  101. static object *
  102. sys_exit(self, args)
  103.     object *self;
  104.     object *args;
  105. {
  106.     /* Raise SystemExit so callers may catch it or clean up. */
  107.     err_setval(SystemExit, args);
  108.     return NULL;
  109. }
  110.  
  111. static object *
  112. sys_settrace(self, args)
  113.     object *self;
  114.     object *args;
  115. {
  116.     if (args == None)
  117.         args = NULL;
  118.     else
  119.         XINCREF(args);
  120.     XDECREF(sys_trace);
  121.     sys_trace = args;
  122.     INCREF(None);
  123.     return None;
  124. }
  125.  
  126. static object *
  127. sys_setprofile(self, args)
  128.     object *self;
  129.     object *args;
  130. {
  131.     if (args == None)
  132.         args = NULL;
  133.     else
  134.         XINCREF(args);
  135.     XDECREF(sys_profile);
  136.     sys_profile = args;
  137.     INCREF(None);
  138.     return None;
  139. }
  140.  
  141. static object *
  142. sys_setcheckinterval(self, args)
  143.     object *self;
  144.     object *args;
  145. {
  146.     if (!newgetargs(args, "i", &sys_checkinterval))
  147.         return NULL;
  148.     INCREF(None);
  149.     return None;
  150. }
  151.  
  152. #ifdef USE_MALLOPT
  153. /* Link with -lmalloc (or -lmpc) on an SGI */
  154. #include <malloc.h>
  155.  
  156. static object *
  157. sys_mdebug(self, args)
  158.     object *self;
  159.     object *args;
  160. {
  161.     int flag;
  162.     if (!getargs(args, "i", &flag))
  163.         return NULL;
  164.     mallopt(M_DEBUG, flag);
  165.     INCREF(None);
  166.     return None;
  167. }
  168. #endif /* USE_MALLOPT */
  169.  
  170. static object *
  171. sys_getrefcount(self, args)
  172.     object *self;
  173.     object *args;
  174. {
  175.     object *arg;
  176.     if (!getargs(args, "O", &arg))
  177.         return NULL;
  178.     return newintobject((long) arg->ob_refcnt);
  179. }
  180.  
  181. #ifdef COUNT_ALLOCS
  182. static PyObject *
  183. sys_getcounts(self, args)
  184.     PyObject *self, *args;
  185. {
  186.     extern PyObject *get_counts Py_PROTO((void));
  187.  
  188.     if (!PyArg_Parse(args, ""))
  189.         return NULL;
  190.     return get_counts();
  191. }
  192. #endif
  193.  
  194. #ifdef Py_TRACE_REFS
  195. /* Defined in objects.c because it uses static globals if that file */
  196. extern PyObject *_Py_GetObjects Py_PROTO((PyObject *, PyObject *));
  197. #endif
  198.  
  199. static struct methodlist sys_methods[] = {
  200.     {"exit",    sys_exit, 0},
  201.     {"getrefcount",    sys_getrefcount, 0},
  202. #ifdef COUNT_ALLOCS
  203.     {"getcounts",    sys_getcounts, 0},
  204. #endif
  205. #ifdef Py_TRACE_REFS
  206.     {"getobjects",    _Py_GetObjects, 1},
  207. #endif
  208. #ifdef USE_MALLOPT
  209.     {"mdebug",    sys_mdebug, 0},
  210. #endif
  211.     {"setprofile",    sys_setprofile, 0},
  212.     {"settrace",    sys_settrace, 0},
  213.     {"setcheckinterval",    sys_setcheckinterval, 1},
  214.     {NULL,        NULL}        /* sentinel */
  215. };
  216.  
  217. static object *sysin, *sysout, *syserr;
  218.  
  219. static object *
  220. list_builtin_module_names()
  221. {
  222.     object *list = newlistobject(0);
  223.     int i;
  224.     if (list == NULL)
  225.         return NULL;
  226.     for (i = 0; inittab[i].name != NULL; i++) {
  227.         object *name = newstringobject(inittab[i].name);
  228.         if (name == NULL)
  229.             break;
  230.         addlistitem(list, name);
  231.         DECREF(name);
  232.     }
  233.     if (sortlist(list) != 0) {
  234.         DECREF(list);
  235.         list = NULL;
  236.     }
  237.     return list;
  238. }
  239.  
  240. void
  241. initsys()
  242. {
  243.     extern long getmaxint PROTO((void));
  244.     extern const char *getversion PROTO((void));
  245.     extern const char *getcopyright PROTO((void));
  246.     extern const char *getplatform PROTO((void));
  247.     extern char *Py_GetPrefix PROTO((void));
  248.     extern char *Py_GetExecPrefix PROTO((void));
  249.     extern int fclose PROTO((FILE *));
  250.     object *m = initmodule("sys", sys_methods);
  251.     object *v;
  252.     sysdict = getmoduledict(m);
  253.     INCREF(sysdict);
  254.     /* NB keep an extra ref to the std files to avoid closing them
  255.        when the user deletes them */
  256.     sysin = newopenfileobject(stdin, "<stdin>", "r", fclose);
  257.     sysout = newopenfileobject(stdout, "<stdout>", "w", fclose);
  258.     syserr = newopenfileobject(stderr, "<stderr>", "w", fclose);
  259.     if (err_occurred())
  260.         fatal("can't initialize sys.std{in,out,err}");
  261.     dictinsert(sysdict, "stdin", sysin);
  262.     dictinsert(sysdict, "stdout", sysout);
  263.     dictinsert(sysdict, "stderr", syserr);
  264.     dictinsert(sysdict, "version", v = newstringobject(getversion()));
  265.     XDECREF(v);
  266.     dictinsert(sysdict, "copyright", v = newstringobject(getcopyright()));
  267.     XDECREF(v);
  268.     dictinsert(sysdict, "platform", v = newstringobject(getplatform()));
  269.     XDECREF(v);
  270.     dictinsert(sysdict, "prefix", v = newstringobject(Py_GetPrefix()));
  271.     XDECREF(v);
  272.     dictinsert(sysdict, "exec_prefix",
  273.            v = newstringobject(Py_GetExecPrefix()));
  274.     XDECREF(v);
  275.     dictinsert(sysdict, "maxint", v = newintobject(getmaxint()));
  276.     XDECREF(v);
  277.     dictinsert(sysdict, "modules", get_modules());
  278.     dictinsert(sysdict, "builtin_module_names",
  279.            v = list_builtin_module_names());
  280.     XDECREF(v);
  281. #ifdef MS_COREDLL
  282.     dictinsert(sysdict, "dllhandle", v = newintobject((int)PyWin_DLLhModule));
  283.     XDECREF(v);
  284.     dictinsert(sysdict, "winver", v = newstringobject(MS_DLL_ID));
  285.     XDECREF(v);
  286. #endif
  287.     if (err_occurred())
  288.         fatal("can't insert sys.* objects in sys dict");
  289. }
  290.  
  291. static object *
  292. makepathobject(path, delim)
  293.     char *path;
  294.     int delim;
  295. {
  296.     int i, n;
  297.     char *p;
  298.     object *v, *w;
  299.     
  300.     n = 1;
  301.     p = path;
  302.     while ((p = strchr(p, delim)) != NULL) {
  303.         n++;
  304.         p++;
  305.     }
  306.     v = newlistobject(n);
  307.     if (v == NULL)
  308.         return NULL;
  309.     for (i = 0; ; i++) {
  310.         p = strchr(path, delim);
  311.         if (p == NULL)
  312.             p = strchr(path, '\0'); /* End of string */
  313.         w = newsizedstringobject(path, (int) (p - path));
  314.         if (w == NULL) {
  315.             DECREF(v);
  316.             return NULL;
  317.         }
  318.         setlistitem(v, i, w);
  319.         if (*p == '\0')
  320.             break;
  321.         path = p+1;
  322.     }
  323.     return v;
  324. }
  325.  
  326. void
  327. setpythonpath(path)
  328.     char *path;
  329. {
  330.     object *v;
  331.     if ((v = makepathobject(path, DELIM)) == NULL)
  332.         fatal("can't create sys.path");
  333.     if (sysset("path", v) != 0)
  334.         fatal("can't assign sys.path");
  335.     DECREF(v);
  336. }
  337.  
  338. static object *
  339. makeargvobject(argc, argv)
  340.     int argc;
  341.     char **argv;
  342. {
  343.     object *av;
  344.     if (argc <= 0 || argv == NULL) {
  345.         /* Ensure at least one (empty) argument is seen */
  346.         static char *empty_argv[1] = {""};
  347.         argv = empty_argv;
  348.         argc = 1;
  349.     }
  350.     av = newlistobject(argc);
  351.     if (av != NULL) {
  352.         int i;
  353.         for (i = 0; i < argc; i++) {
  354.             object *v = newstringobject(argv[i]);
  355.             if (v == NULL) {
  356.                 DECREF(av);
  357.                 av = NULL;
  358.                 break;
  359.             }
  360.             setlistitem(av, i, v);
  361.         }
  362.     }
  363.     return av;
  364. }
  365.  
  366. void
  367. setpythonargv(argc, argv)
  368.     int argc;
  369.     char **argv;
  370. {
  371.     object *av = makeargvobject(argc, argv);
  372.     object *path = sysget("path");
  373.     if (av == NULL)
  374.         fatal("no mem for sys.argv");
  375.     if (sysset("argv", av) != 0)
  376.         fatal("can't assign sys.argv");
  377.     if (path != NULL) {
  378.         char *p = NULL;
  379.         int n = 0;
  380.         object *a;
  381. #if SEP == '\\' /* Special case for MS filename syntax */
  382.         if (argc > 0 && argv[0] != NULL) {
  383.             char *q;
  384.             p = strrchr(argv[0], SEP);
  385.             /* Test for alternate separator */
  386.             q = strrchr(p ? p : argv[0], '/');
  387.             if (q != NULL)
  388.                 p = q;
  389.             if (p != NULL) {
  390.                 n = p + 1 - argv[0];
  391.                 if (n > 1 && p[-1] != ':')
  392.                     n--; /* Drop trailing separator */
  393.             }
  394.         }
  395. #else /* All other filename syntaxes */
  396.         if (argc > 0 && argv[0] != NULL)
  397.             p = strrchr(argv[0], SEP);
  398.         if (p != NULL) {
  399.             n = p + 1 - argv[0];
  400. #if SEP == '/' /* Special case for Unix filename syntax */
  401.             if (n > 1)
  402.                 n--; /* Drop trailing separator */
  403. #endif /* Unix */
  404.         }
  405. #ifdef _AMIGA
  406.         else
  407.         {
  408.             /* check for absolute paths on Amiga */
  409.             if(argc>0 && argv[0]!=NULL) p=strrchr(argv[0],':');
  410.             if(p!=NULL)    n=p+1-argv[0];
  411.         }
  412. #endif /* _AMIGA */
  413. #endif /* All others */
  414.         a = newsizedstringobject(argv[0], n);
  415.         if (a == NULL)
  416.             fatal("no mem for sys.path insertion");
  417.         if (inslistitem(path, 0, a) < 0)
  418.             fatal("sys.path.insert(0) failed");
  419.         DECREF(a);
  420.     }
  421.     DECREF(av);
  422. }
  423.